<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: red;
            margin: 50px;
        }
        div:nth-child(4){
            background-color: blue;
        }
    </style>
</head>
<body>
    <button>开关1</button>
    <button>开关2</button>
    <div></div>
    <div></div>

    <script>
        var but = document.querySelectorAll("button");
        var oDiv = document.querySelectorAll("div");

        for(var i=0; i<but.length; i++){
            but[i].tag = true;
            but[i].index = i;
            but[i].onclick = function(){
                if(this.tag == true){
                    oDiv[this.index].style.display = 'none';
                    this.tag = false;
                }else{
                    oDiv[this.index].style.display = 'block';
                    this.tag = true;
                }
            }
        }
    </script>
</body>
</html>